class Solution {
public:
    int longestSubarray(vector<int>& nums) {
        nums.insert(nums.begin(), 0);
        for (int i = 1; i < nums.size(); i++) nums[i] += nums[i - 1];
        int l = 1, r = nums.size() - 1;
        while (l < r)
        {
            int mid = (l + r) / 2 + 1;
            int head = 1, tail = mid;
            int flag = 1;
            while (tail < nums.size())
            {
                if (nums[tail] - nums[head - 1] >= mid - 1)
                {
                    flag = 0;
                    l = mid;
                    break;
                }
                head += 1, tail += 1;
            }
            if (flag) r = mid - 1;
        }
        return l - 1;
    }
};